package net.member.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class MemberDAO {
	
	DataSource ds;
	ResultSet rs = null;
	Connection con = null;
	PreparedStatement pstmt = null;
	MemberBean memberbean = new MemberBean();
	
	public MemberDAO() throws Exception{
		try {
			Context init = new InitialContext();
			ds=(DataSource)init.lookup("java:comp/env/jdbc/shopping");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
//	DB연결 유무 판단 시작
	private void dbClose(){
		if(rs!=null)try{rs.close();}catch(SQLException ex){}
		if(pstmt!=null)try{pstmt.close();}catch(SQLException ex){}
		if(con!=null)try{con.close();}catch(SQLException ex){}
		System.out.println("Shopping Member DB Closed");
	}
//	DB연결 유무 판단 종료

	public int useCheck(String member_id, String member_pw) throws Exception {
		
		String sql = null;
		int x = -1;
				
			try {
				System.out.println("Login UserCheck Start");
				con = ds.getConnection();
				sql = "select member_pw from member where member_id = ?";
				pstmt=con.prepareStatement(sql);
				pstmt.setString(1, member_id);
				rs=pstmt.executeQuery();
				
				if(rs.next()){
					String memberpw = rs.getString("member_pw");
					
					if(memberpw.equals(member_pw)){
						x = 1;
					}else{
						x = 0;
					}
				System.out.println("Login UserCheck End");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				if(rs!=null) rs.close();
				if(pstmt!=null) pstmt.close();
				if(con!=null) con.close();
			}dbClose();
		return x;
	}

	public boolean isAdmin(String member_id) throws SQLException {
		
		String sql = null;
		
		try {
			con = ds.getConnection();
			sql = "select member_admin from member where member_id=?";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, member_id);
			rs = pstmt.executeQuery();	
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(rs!=null) rs.close();
			if(pstmt!=null) pstmt.close();
			if(con!=null) con.close();
		}dbClose();
		return false;
	}

	public boolean insertMember(MemberBean mb) throws Exception {
		
		String sql = null;
		boolean result = false;
		try {
			con = ds.getConnection();
			sql = "insert into member values"+
				  "(?,?,?,?,?,?,?,?,?,?,?,?,?)";
			pstmt = con.prepareStatement(sql);
			pstmt.setString(1, mb.getMember_id());
			pstmt.setString(2, mb.getMember_pw());
			pstmt.setString(3, mb.getMember_name());
			pstmt.setInt(4, mb.getMember_jumin1());
			pstmt.setInt(5, mb.getMember_jumin2());
			pstmt.setString(6, mb.getMember_email());
			pstmt.setString(7, mb.getMember_email_get());
			pstmt.setString(8, mb.getMember_phone());
			pstmt.setString(9, mb.getMember_zipcode());
			pstmt.setString(10, mb.getMember_addr1());
			pstmt.setString(11, mb.getMember_addr2());
			pstmt.setInt(12, mb.getMember_admin());
			pstmt.setDate(13, mb.getMember_join_date());
			
			pstmt.executeUpdate();
			result = true;
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(rs!=null) rs.close();
			if(pstmt!=null) pstmt.close();
			if(con!=null) con.close();
		}dbClose();
		return result;
	}
}
